package org.example.top40;

public class main21 {
    //环形链表 输入：head = [3,2,0,-4], pos = 1 输出：true
    //思路；快慢指针
    public static void main(String[] args) {
        ListNode head = builder();
        boolean isCycle = isCycle(head);
        System.out.println(isCycle);
    }
    private static boolean isCycle(ListNode head){
        ListNode slow=head;
        ListNode fast=head;
        while (fast!=null&&fast.next!=null){
            slow=slow.next;
            fast=fast.next.next;
            if(slow==fast){
                return true;
            }
        }
        return false;

    }
    private static ListNode builder(){
        ListNode head = new ListNode(3);
        ListNode node1 = new ListNode(2);
        ListNode node2 = new ListNode(0);
        ListNode node3 = new ListNode(-4);
        head.next=node1;
        node1.next=node2;
        node2.next=node3;
        node3.next=node1;
        return head;
    }
}
